package cn.tedu.common.pojo;

import java.util.List;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class EasyUIResult {

	// 定义jackson对象
	private static final ObjectMapper MAPPER = new ObjectMapper();
	// 查询数据总数量
	private Integer total;
	// 分页查询的list结果
	private List<?> rows;

	public EasyUIResult(Long total, List<?> rows) {
		this.total = total.intValue();
		this.rows = rows;
	}

	/**
	 * Object是集合转化
	 * 
	 * @param jsonData
	 *            json数据
	 * @param clazz
	 *            集合中的类型
	 * @return
	 */
	public static EasyUIResult formatToList(String jsonData, Class<?> clazz) {
		try {
			JsonNode jsonNode = MAPPER.readTree(jsonData);
			JsonNode data = jsonNode.get("rows");
			List<?> list = null;
			if (data.isArray() && data.size() > 0) {
				list = MAPPER.readValue(data.traverse(),
						MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));
			}
			return new EasyUIResult(jsonNode.get("total").intValue(), list);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
